﻿using SmartCMS.UI.Helper;
using SmartCMS.UI.ViewModels;
using SmartControls;
using SmartControls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SmartCMS.UI.SystemManage
{
    public partial class EditMenuForm : MetroForm
    {
        public EditMenuForm()
        {
            InitializeComponent();
        }

        private void EditMenuForm_Load(object sender, EventArgs e)
        {
            var menus = DBHelper.GetCollection<MenuItemEntity>().Find(x=>x.PID==0).ToList();
            Menuslevel.DataSource = menus;
            Menuslevel.ValueMember = "ID";
            Menuslevel.DisplayMember = "MenuName";
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            var menus = DBHelper.GetCollection<MenuItemEntity>();
            MenuItemEntity model = new MenuItemEntity();
            model.ID = menus.FindAll().Max(x => x.ID) + 1;
            model.PID =Convert.ToInt32(Menuslevel.SelectedValue);
            model.MenuName = MenuName.Text.Trim();
            model.WindowName = WindowName.Text.Trim();
            model.OpenType = ShowType.Text;
            //model.Visble = iSVisble.Text;
            if (menus.FindOne(x=>x.MenuName == model.MenuName)==null)
            {
                try
                {
                    menus.Insert(model);
                    MetroMessageBox.Show(this, "保存成功！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch
                {
                    MetroMessageBox.Show(this, "保存失败！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }               
            }
            else
            {
                MetroMessageBox.Show(this, "该信息已存在！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
